var images = require("images");
const path = require("path")
const exec = require('child_process').exec;
const { readdir, rename } = require('fs/promises')

async function start() {
  // 删除文件内文件
  const delDir = new Promise((resolve, reject) => {
    exec("del /F /S /Q output", function () {
      resolve()
    });
  })
  await delDir
  // 获取所有图片地址
  let imgDir = await readdir(path.resolve(__dirname, "./img"))
  //  绝对路径处理
  imgDir = imgDir.map(item => path.resolve(__dirname, "./img", item))

  for (const imgPath of imgDir) {
    const savePath = path.resolve(__dirname, "output", path.basename(imgPath).replace(".png", ".jpg"))
    images(imgPath)                     //Load image from file
      //保存
      .save(savePath, {               //Save the image to a file, with the quality of 50
        quality: 90                    //保存图片到文件,图片质量为50
      });
    // 文件重命名
    const imgRename = savePath.replace(".jpg", ".png")
    await rename(savePath, imgRename)
    console.log(savePath, imgRename)
  }
}

start()
